package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 更新地理位置
     * @param userId 用户id
     * @param longitude  经度
     * @param latitude   纬度
     * @param address  位置描述
     * @return
     */
    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String address) {
        try {
            //1.根据用户id查询地理位置信息
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            //2.判断位置信息是否存在
            if (location == null) {
                //不存在就新增一个地理信息
                location = new UserLocation();
                location.setUserId(userId);
                location.setAddress(address);
                location.setLocation(new GeoJsonPoint(longitude, latitude));
                location.setCreated(System.currentTimeMillis());
                location.setUpdated(System.currentTimeMillis());
                location.setLastUpdated(System.currentTimeMillis());
                mongoTemplate.save(location);
            }
            //3.存在就更新
            Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
                    .set("updated", System.currentTimeMillis())
                    .set("lastUpdateed", location.getUpdated());
            mongoTemplate.updateFirst(query, update, UserLocation.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据位置查询附近人的信息
     * @param userId 当前用户id
     * @param metre
     * @return
     */
    @Override
    public List<Long> queryNearUser(Long userId, Double metre) {
        //1.根据当前用户id用户地理位置
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        //2.如果查询为空就返回空
        if (location == null) {
            return null;
        }
        //3.以当前用户绘制圆点
        GeoJsonPoint point = location.getLocation();
        //4.绘制半径
        Distance distance = new Distance(metre / 1000, Metrics.KILOMETERS);
        //5.绘制圆形
        Circle circle = new Circle(point, distance);
        //6.查询 符合圆形地理位置的用户
        Query locationQuery = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);
        //7.获取所有userLocations的用户id 并返回
        List<Long> longs = CollUtil.getFieldValues(userLocations, "userId", Long.class);
        return longs;
    }
}
